package com.bjb.vr.common.utils;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Objects;


/**
 * @description: AliyunOss操作工具类
 * @author: HuJingBo
 * @time: 2022/2/16 20:18
 */
@Slf4j
@Component
public class AliyunOssUtil {

    private static final String ASSERT_MESSAGE = "The ossClient is not initialized. Please initialize first";
    private static String bucketName = "xr-cloud1";
    private static OSS ossClient = null;

    private AliyunOssUtil() {
    }

    public static class Builder {

        /**
         * 获取阿里云OSS客户端对象
         *
         * @param endpoint
         * @param accessKeyId
         * @param accessKeySecret
         * @return
         */
        public AliyunOssUtil build(String endpoint, String accessKeyId, String accessKeySecret) {
            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            return new AliyunOssUtil();
        }
    }


    /**
     * 创建存储空间
     *
     * @param bucketName 存储空间
     * @return
     */
    public String createBucketName(String bucketName) {
        Assert.notNull(ossClient, ASSERT_MESSAGE);
        String bucketNames = bucketName;
        if (!ossClient.doesBucketExist(bucketName)) {
            // 创建存储空间
            Bucket bucket = ossClient.createBucket(bucketName);
            log.info("创建Bucket成功");
            return bucket.getName();
        }
        return bucketNames;
    }

    /**
     * 删除存储空间buckName
     *
     * @param bucketName 存储空间
     */
    public void deleteBucket(String bucketName) {
        Assert.notNull(ossClient, ASSERT_MESSAGE);
        ossClient.deleteBucket(bucketName);
        log.info("删除Bucket: {}成功", bucketName);
    }

    /**
     * 删除OSS服务器上的文件
     *
     * @param fileId 文件名
     */
    public void deleteFile(String fileId) {
        Assert.notNull(ossClient, ASSERT_MESSAGE);
        ossClient.deleteObject(bucketName, fileId);
        log.info("删除{}下的文件{}成功", bucketName, fileId);
    }

    /**
     * @param file   文件
     * @param fileId
     * @return
     */
    public String upload(File file, String fileId) {
        Assert.notNull(ossClient, ASSERT_MESSAGE);
        String resultStr = null;
        try (InputStream ips = new FileInputStream(file);) {
            String extension = FilenameUtils.getExtension(file.getPath());
            String fileFullName = fileId + "." + extension;
            ossClient.putObject(bucketName, fileFullName, ips);
            resultStr = this.getFileUrl(fileFullName);
        } catch (Exception e) {
            log.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
        }
        return resultStr;
    }

    /**
     * 获得url链接
     *
     * @param fileId
     * @return
     */
    public String getFileUrl(String fileId) {
        Assert.notNull(ossClient, ASSERT_MESSAGE);
        Date expiration = DateTimeUtil.getNextDate(7);
        // 生成签名URL。
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, fileId, HttpMethod.GET);
        // 设置过期时间。
        request.setExpiration(expiration);
        // 通过HTTP GET请求生成签名URL。
        URL signedUrl = ossClient.generatePresignedUrl(request);
        if (!Objects.isNull(signedUrl)) {
            return signedUrl.toString();
        }
        return null;
    }
}
